package org.luo.user.domain.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.luo.common.common.Result;
import org.luo.common.utils.JwtUtil;
import org.luo.common.utils.Md5Util;
import org.luo.common.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    @Override
    public User findByUserName(String username) {
        User entity = lambdaQuery().eq(true, User::getUsername, username).one();

        return entity;
    }

    @Override
    public Result register(String username, String password) {
        User u = lambdaQuery().eq(true, User::getUsername, username).one();
        if (u == null) {
            String md5 = Md5Util.getMD5String(password);
            u = new User();
            u.setUsername(username);
            u.setPassword(md5);
            save(u);
            return Result.success();
        } else {
            return Result.error("用户名已被占用");
        }
    }

    @Override
    public Result<String> login(String username, String password) {


        User loginUser = lambdaQuery().eq(true, User::getUsername, username).one();
//        LambdaQueryWrapper<User> wrap = new LambdaQueryWrapper<>();
//        wrap.eq(true, User::getUsername, username);
//        User loginUser = getOne(wrap);

        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map< String, Object > claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
//            ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
//            operations.set(token,token,12, TimeUnit.HOURS);
            return Result.success(token);

        }
        return Result.error("密码错误");
    }

    @Override
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username =(String) map.get("username");
        User user = lambdaQuery().eq(true, User::getUsername, username).one();
        return Result.success(user);
    }

    @Override
    public Result update(User user) {
        updateById(user);
        return Result.success();
    }

    @Override
    public Result updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User u = new User();
        u.setId(id);
        u.setUserPic(avatarUrl);
        updateById(u);
        return Result.success();
    }

    @Override
    public Result updatePwd(String oldPwd, String newPwd, String rePwd) {
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
            return Result.error("参数错误");
        }
        if(!newPwd.equals(rePwd)){
            return Result.error("密码不一致");
        }
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id =(Integer) map.get("id");
        User u = getById(id);

        if(!u.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码错误");
        }

        u = new User();
        u.setId(id);
        u.setPassword(Md5Util.getMD5String(newPwd));
        updateById(u);

//        ValueOperations<String, String> op = stringRedisTemplate.opsForValue();
//        op.getOperations().delete(token);
        return Result.success();
    }

    @Override
    public Result<Page<User>> getList(Integer pageNum, Integer pageSize) {

        Page<User> page = Page.of(pageNum, pageSize);
        lambdaQuery().page(page);

        return Result.success(page) ;
    }
}
